﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cms.Entity.Menu;
using Cms.BLL.Article;
using Frame.Lib.Core.Attach;
using Frame.Lib.Utilities;

namespace Cms.Admin.Picture
{
    public partial class GroupPictureEdit : PageEdit
    {
        private string ArticleID
        {
            get
            {
                return Request["ArticleID"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
          
            ImageBO picture = new ImageBO("CmsArticlePicture");


            ArticleBO article = new ArticleBO();
            MenuInfo menuInfo = article.GetInfo(ArticleID).MenuInfo;
           
            //判断图片格式
            string extention = System.IO.Path.GetExtension(this.fileTitlePicture.FileName).ToLower();
            if (extention == ".jpg" || extention == ".png" || extention == ".gif" || extention == ".bmp")
            {
                System.Drawing.Bitmap img = new System.Drawing.Bitmap(fileTitlePicture.PostedFile.InputStream);
                ImageInfo pictureInfo = new ImageInfo();
                pictureInfo.Name = System.IO.Path.GetFileName(fileTitlePicture.FileName);
                pictureInfo.BindID = int.Parse(ArticleID);
                pictureInfo.InputTime = DateTime.Now;
                pictureInfo.Width = img.Width;
                pictureInfo.Height = img.Height;
                pictureInfo.Size = (int)fileTitlePicture.PostedFile.InputStream.Length;
                pictureInfo.Orders = picture.GetMaxOrder(pictureInfo.BindID);

                article.SaveImage(pictureInfo, fileTitlePicture.PostedFile, menuInfo);

                try
                {

                    picture.Save(pictureInfo);
                }
                catch
                {
                    JSUtil.Alert("保存数据库出错");
                    return;

                }
                JSUtil.RefreshView();
            }
            else
            {
                JSUtil.Alert("图片格式不对");
            }
        }
    }
}
